using System.Data;
using MySql.Data.MySqlClient;

namespace ODT.Intellisense.Models
{
	internal static class LocalCommand
	{
		internal static MySqlCommand CreateAliasCommand(string connectionString, string word)
		{
			MySqlCommand mySqlCommand = CreateCommand(connectionString);
			mySqlCommand.CommandText = "Select alias from software_tbl0 where software_tbl0.command like ?word";
			mySqlCommand.Parameters.Add("?word", MySqlDbType.VarChar).Value = word;
			return mySqlCommand;
		}

		internal static MySqlCommand CreateHelpCommand0(string connectionString, string suggestion)
		{
			MySqlCommand mySqlCommand = CreateCommand(connectionString);
			mySqlCommand.CommandText = "SELECT software_tbl0.Comments FROM software_tbl0 WHERE software_tbl0.Command =?suggestion";
			mySqlCommand.Parameters.Add("?suggestion", MySqlDbType.VarChar).Value = suggestion;
			return mySqlCommand;
		}

		internal static MySqlCommand CreateHelpCommand1(string connectionString, string suggestion)
		{
			MySqlCommand mySqlCommand = CreateCommand(connectionString);
			mySqlCommand.CommandText = "SELECT software_tbl1.Comments FROM software_tbl1 WHERE software_tbl1.Command = ?suggestion";
			mySqlCommand.Parameters.Add("?suggestion", MySqlDbType.VarChar).Value = suggestion;
			return mySqlCommand;
		}

		internal static MySqlCommand CreateHelpCommand2(string connectionString, string suggestion)
		{
			MySqlCommand mySqlCommand = CreateCommand(connectionString);
			mySqlCommand.CommandText = "SELECT software_tbl2.Comments FROM software_tbl2 WHERE software_tbl2.Command = ?suggestion";
			mySqlCommand.Parameters.Add("?suggestion", MySqlDbType.VarChar).Value = suggestion;
			return mySqlCommand;
		}

		internal static MySqlCommand CreateLevel1Command0(string connectionString, string level1Command)
		{
			MySqlCommand mySqlCommand = CreateCommand(connectionString);
			mySqlCommand.CommandText = "SELECT Command FROM software_tbl1 WHERE commandID LIKE ?level1Command and UserLevel < 4 ORDER BY Command";
			mySqlCommand.Parameters.Add("?level1Command", MySqlDbType.VarChar).Value = level1Command;
			return mySqlCommand;
		}

		internal static MySqlCommand CreateLevel1Command1(string connectionString, string level1Command)
		{
			MySqlCommand mySqlCommand = CreateCommand(connectionString);
			mySqlCommand.CommandText = "SELECT Name FROM userprog WHERE userprog.Type LIKE ?level1Command";
			mySqlCommand.Parameters.Add("?level1Command", MySqlDbType.VarChar).Value = level1Command;
			return mySqlCommand;
		}

		internal static MySqlCommand CreateLevel1Command2(string connectionString, string level1Command)
		{
			MySqlCommand mySqlCommand = CreateCommand(connectionString);
			mySqlCommand.CommandText = "SELECT Name FROM userglobal Where userglobal.Type LIKE ?level1Command";
			mySqlCommand.Parameters.Add("?level1Command", MySqlDbType.VarChar).Value = level1Command;
			return mySqlCommand;
		}

		internal static MySqlCommand CreateLevel2Command0(string connectionString, string level1Command, string level2Command)
		{
			MySqlCommand mySqlCommand = CreateCommand(connectionString);
			mySqlCommand.CommandText = "SELECT software_tbl2.Command FROM software_tbl1, software_tbl2 WHERE  software_tbl1.Command = software_tbl2.CommandId AND software_tbl1.Command = ?level2Command and software_tbl1.commandID = ?level1Command and (CASE WHEN software_tbl2.GateChan = 'NULL' THEN software_tbl2.GateChan = 'NULL'  ELSE software_tbl2.GateChan = ?level1Command1 END ) and software_tbl2.UserLevel < 4 Order By software_tbl2.Command";
			mySqlCommand.Parameters.Add("?level1Command", MySqlDbType.VarChar).Value = level1Command;
			mySqlCommand.Parameters.Add("?level1Command1", MySqlDbType.VarChar).Value = level1Command;
			mySqlCommand.Parameters.Add("?level2Command", MySqlDbType.VarChar).Value = level2Command;
			return mySqlCommand;
		}

		internal static MySqlCommand CreateLevel2Command0GateChan(string connectionString, string level1Command, string level2Command)
		{
			MySqlCommand mySqlCommand = CreateCommand(connectionString);
			mySqlCommand.CommandText = "SELECT software_tbl2.Command FROM software_tbl1, software_tbl2 WHERE software_tbl1.Command = software_tbl2.CommandId AND Match(software_tbl1.Command)Against(?level2Command In Boolean Mode) and software_tbl1.commandID =?level1Command and software_tbl2.GateChan = ?level1Command1 and software_tbl2.UserLevel < 4 Order By software_tbl2.Command";
			mySqlCommand.Parameters.Add("?level1Command", MySqlDbType.VarChar).Value = level1Command;
			mySqlCommand.Parameters.Add("?level1Command1", MySqlDbType.VarChar).Value = level1Command;
			mySqlCommand.Parameters.Add("?level2Command", MySqlDbType.VarChar).Value = level2Command + "*";
			return mySqlCommand;
		}

		internal static MySqlCommand CreateLevel2Command1(string connectionString, string level1Command, string level2Command)
		{
			MySqlCommand mySqlCommand = CreateCommand(connectionString);
			mySqlCommand.CommandText = "Select userlocal.Name from userlocal,userprog where userlocal.ProgId = userprog.ProgId and Match(userprog.ProgID) Against(CONCAT(?level1Command,?level2Command) In Boolean Mode)";
			mySqlCommand.Parameters.Add("?level1Command", MySqlDbType.VarChar).Value = level1Command;
			mySqlCommand.Parameters.Add("?level2Command", MySqlDbType.VarChar).Value = level2Command;
			return mySqlCommand;
		}

		internal static MySqlCommand CreateLevel3Command0(string connectionString, string level1Command, string level2Command, string level3Command)
		{
			MySqlCommand mySqlCommand = CreateCommand(connectionString);
			mySqlCommand.CommandText = "SELECT software_tbl3.Command FROM software_tbl1, software_tbl2, software_tbl3  WHERE  software_tbl1.Command = software_tbl2.CommandId AND software_tbl1.Command = ?level2Command and software_tbl1.commandID = ?level1Command AND software_tbl2.Command = software_tbl3.CommandId AND  software_tbl2.Command = ?level3Command AND  software_tbl2.commandID = ?level2Command1 and software_tbl3.UserLevel < 4 Order By software_tbl3.Command";
			mySqlCommand.Parameters.Add("?level1Command", MySqlDbType.VarChar).Value = level1Command;
			mySqlCommand.Parameters.Add("?level2Command", MySqlDbType.VarChar).Value = level2Command;
			mySqlCommand.Parameters.Add("?level2Command1", MySqlDbType.VarChar).Value = level2Command;
			mySqlCommand.Parameters.Add("?level3Command", MySqlDbType.VarChar).Value = level3Command;
			return mySqlCommand;
		}

		internal static MySqlCommand CreateCommand(string connectionString)
		{
			return new MySqlCommand
			{
				CommandType = CommandType.Text,
				Connection = new MySqlConnection(connectionString)
			};
		}
	}
}
